package cn.edu.scau.cmi.zhangjunming.spring.addHibernate;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

import cn.edu.scau.cmi.zhangjunming.hibernate.DAO.StudentDAO;
import cn.edu.scau.cmi.zhangjunming.hibernate.DAO.TeacherDAO;
import cn.edu.scau.cmi.zhangjunming.hibernate.domain.Student;
import cn.edu.scau.cmi.zhangjunming.hibernate.domain.Teacher;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class SpringAddHibController implements Initializable{

	private static Stage stage = null;
	private static Scene scene = null;
	@FXML private TextField stuId;
	@FXML private TextField stuName;
	@FXML private TextField teaId;
	@FXML private TextField teaName;
	@FXML private Button addStu;
	@FXML private Button changeStu;
	@FXML private Button deleteStu;
	@FXML private Button addTea;
	@FXML private Button deleteTea;
	@FXML private Button changeTea;
	@FXML private ComboBox stuBox;
	@FXML private ComboBox teaBox;
	@FXML private TextArea result;
    @FXML private Button stuShow;
    @FXML private Button teaShow;
	
	List<Student> stuList;
	List<Teacher> teaList;
	
	private static final StudentDAO studentDAO = (StudentDAO)ApplicationContextUtil.getApplicationContext().getBean("studentDAO");
	private static final TeacherDAO teacherDAO = (TeacherDAO)ApplicationContextUtil.getApplicationContext().getBean("teacherDAO");
	
	public SpringAddHibController() {
		
	}

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		refreshStuBox();
		refreshTeaBox();
		
	}
	
	static void createScene() {
		if(scene==null) {
			try {
				URL url = new SpringAddHibController().getClass().getResource("SpringaddHibernate.fxml");
				Parent root = FXMLLoader.load(url);
				scene = new Scene(root);
			}catch(IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static Stage getStage() {
		return stage;
	}

	public static void setStage(Stage stage) {
		SpringAddHibController.stage = stage;
	}
	
	private void refreshStuBox() {
		stuBox.getItems().remove(0, stuBox.getItems().size());
		stuList = studentDAO.listAll();
		for(int i=0;i<stuList.size();i++) {
			Student student = (Student)stuList.get(i);
			String s = new String(student.getId()+" "+student.getName());
			stuBox.getItems().add(s);
		}
	}
	
	private void refreshTeaBox() {
		teaBox.getItems().remove(0,teaBox.getItems().size());
		teaList = teacherDAO.listAll();
		for(int i=0;i<teaList.size();i++) {
			Teacher teacher = (Teacher)teaList.get(i);
			String s = new String(teacher.getId()+" "+teacher.getName());
			teaBox.getItems().add(s);
		}
	}
	
	private Student getSelectedStu() {
		int index = stuBox.getSelectionModel().getSelectedIndex();
		Student stu = stuList.get(index);
		return stu;
	}
	
	private Teacher getSelectedTea() {
		int index = teaBox.getSelectionModel().getSelectedIndex();
		Teacher tea = teaList.get(index);
		return tea;
	}
	
	@FXML private void stu_add(){
		long id = 0;
		String name = null;
		id = Long.parseLong(stuId.getText()); 
		name = stuName.getText();
		if(name==null||name.equals("")||stuId.getText().equals("")) {
			System.out.println("录入数据非法！\n");
			return;
		}
		
		if(studentDAO.sameId(id)) {
			System.out.println("录入的id有冲突！\n");
			return;
		}
		
		Student stu = new Student();
		stu.setId((int)id);
		stu.setName(name);
		stu.setTutor(null);
	    studentDAO.save(stu);
	    result.clear();
	    result.appendText(new String("学生 "+stu.getName()+"的信息已录入"));
		refreshStuBox();
	}
	
	@FXML private void stu_change() {
		int index = stuBox.getSelectionModel().getSelectedIndex();
		Student stu = stuList.get(index);
		String name = stuName.getText();
		if(name==null||name.equals("")) {
			System.out.println("姓名不能为空！");
			return;
		}
		stu.setName(name);
		studentDAO.update(stu);
		result.clear();
		result.appendText(new String("学生 "+stu.getName()+"的信息已更新"));
		refreshStuBox();
	}
	
	@FXML private void stu_delete() {
		int index = stuBox.getSelectionModel().getSelectedIndex();
		Student stu = stuList.get(index);
		studentDAO.delete(stu);
		result.clear();
		result.appendText("学生信息已删除\n");
		refreshStuBox();
	}
	
	@FXML private void show_stu() {
		int index = stuBox.getSelectionModel().getSelectedIndex();
		Student stu = stuList.get(index);
		result.clear();
		result.appendText("学生信息：\n 学生id:"+stu.getId()+"学生姓名："+stu.getName()+"\n导师姓名：");
		if(stu.getTutor()!=null) {
			result.appendText(stu.getTutor().getName());
		}
		else {
			result.appendText("(无导师)");
		}
	}
	
	@FXML private void tea_add() {
		long id =0;
		String name = null;
		id =Long.parseLong(teaId.getText());
		name = teaName.getText();
		if(name==null||name.equals("")) {
			System.out.println("录入数据非法!");
			return;
		}
		if(studentDAO.sameId(id)) {
			System.out.println("id重复！");
			return;
		}
	    Teacher tea = new Teacher();
	    tea.setId((int)id);
	    tea.setName(name);
	    teacherDAO.save(tea);
	    result.clear();
	    result.appendText(new String("教师 "+tea.getName()+"的信息已录入"));
	    refreshTeaBox();
	}

	@FXML private void tea_change() {
		int index = teaBox.getSelectionModel().getSelectedIndex();
		Teacher tea = teaList.get(index);
		long id = Long.parseLong( teaId.getText());
		String name = teaName.getText();
		if(name==null||name.equals("")) {
			System.out.println("录入数据非法!");
			return;
		}
		if(studentDAO.sameId(id)) {
			System.out.println("id重复！");
			return;
		}
		tea.setId((int)id);
		tea.setName(name);
		teacherDAO.update(tea);
		result.clear();
		result.appendText(new String("教师信息已修改！\n"));
		refreshTeaBox();
	}
	
	@FXML private void tea_delete() {
		int index = teaBox.getSelectionModel().getSelectedIndex();
		Teacher tea = teaList.get(index);
		teacherDAO.delete(tea);
		result.clear();
		result.appendText("教师信息已删除\n");
		refreshTeaBox();
	}

	@FXML private void show_tea() {
		int index = teaBox.getSelectionModel().getSelectedIndex();
		Teacher tea = teaList.get(index);
		result.clear();
		result.appendText("教师信息：\n 教师id:"+tea.getId()+"教师姓名："+tea.getName());
	}

	@FXML private void set_tut() {
		int stu_index = stuBox.getSelectionModel().getSelectedIndex();
		Student stu = stuList.get(stu_index);
		int tea_index = teaBox.getSelectionModel().getSelectedIndex();
		Teacher tea = teaList.get(tea_index);
		stu.setTutor(tea);
		studentDAO.update(stu);
		result.clear();
		result.appendText("学生信息已修改:\n学生 "+stu.getName()+" 的导师修改为 "+tea.getName());
		refreshStuBox();
		refreshTeaBox();
	}
	
	@FXML private void set_stu() {
		int stu_index = stuBox.getSelectionModel().getSelectedIndex();
		Student stu = stuList.get(stu_index);
		int tea_index = teaBox.getSelectionModel().getSelectedIndex();
		Teacher tea = teaList.get(tea_index);
		stu.setTutor(tea);
		studentDAO.update(stu);
		result.clear();
		result.appendText("教师 "+tea.getName()+"选择学生 "+stu.getName()+"来指导");
		refreshStuBox();
		refreshTeaBox();
	}
}
